package com.smart.translator.ui.device;

import android.bluetooth.BluetoothDevice;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.smart.translator.R;
import java.util.ArrayList;
import java.util.List;

public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.DeviceViewHolder> {
    private List<BluetoothDevice> devices = new ArrayList<>();
    private List<Integer> rssiValues = new ArrayList<>();
    private OnDeviceClickListener listener;

    public interface OnDeviceClickListener {
        void onDeviceClick(BluetoothDevice device);
    }

    public DeviceAdapter(OnDeviceClickListener listener) {
        this.listener = listener;
    }

    public void updateDevices(List<BluetoothDevice> newDevices) {
        this.devices = newDevices;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public DeviceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_device, parent, false);
        return new DeviceViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DeviceViewHolder holder, int position) {
        BluetoothDevice device = devices.get(position);
        int rssi = position < rssiValues.size() ? rssiValues.get(position) : 0;
        holder.bind(device, rssi);
    }

    @Override
    public int getItemCount() {
        return devices.size();
    }

    public void addDevice(BluetoothDevice device, int rssi) {
        if (!devices.contains(device)) {
            devices.add(device);
            rssiValues.add(rssi);
            notifyItemInserted(devices.size() - 1);
        }
    }

    public void clearDevices() {
        devices.clear();
        rssiValues.clear();
        notifyDataSetChanged();
    }

    public BluetoothDevice getDevice(int position) {
        return devices.get(position);
    }

    class DeviceViewHolder extends RecyclerView.ViewHolder {
        private TextView tvDeviceName;
        private TextView tvDeviceAddress;
        private TextView tvDeviceRssi;

        public DeviceViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDeviceName = itemView.findViewById(R.id.tv_device_name);
            tvDeviceAddress = itemView.findViewById(R.id.tv_device_address);
            tvDeviceRssi = itemView.findViewById(R.id.tv_device_rssi);

            itemView.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION) {
                    listener.onDeviceClick(devices.get(position));
                }
            });
        }

        public void bind(BluetoothDevice device, int rssi) {
            String name = device.getName();
            tvDeviceName.setText(name != null ? name : "未知设备");
            tvDeviceAddress.setText(device.getAddress());
            tvDeviceRssi.setText(String.format("信号强度: %d dBm", rssi));
        }
    }
} 